package com.example.cet4try.myDialog;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;

import com.example.cet4try.R;
import com.example.cet4try.Utils.Tools;
import com.example.cet4try.Utils.WordData;
import com.example.cet4try.diyView.TopView;

public class WordDetailsDialog extends DialogFragment {

//    private String[] data;
    private WordData wordData;
    private ImageView collection;
    private PlaySound playSound = null;
    private String username;
    private String title;
    private int isCollect;
    private boolean closeDialogOnly = false;
    private boolean isShow = false;
    private ImageView play_sound;
    /*private LinearLayout buttons;
    private Button t_continue;*/
    private boolean isFuxi = false;

    public WordDetailsDialog(){

    }

    public WordDetailsDialog(String title){
        this.title = title;
    }

    public WordDetailsDialog(String title,boolean closeDialogOnly){
        this.title = title;
        this.closeDialogOnly = closeDialogOnly;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NO_FRAME, android.R.style.Theme_Holo_Light);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.word_details_dialog,container,false);

        TopView topView = v.findViewById(R.id.topview);
        topView.setmTitle(title);

        LinearLayout buttons = v.findViewById(R.id.buttons);
        if (closeDialogOnly){
            topView.setmDialog(getDialog());

            buttons.setVisibility(View.GONE);
            LinearLayout body = v.findViewById(R.id.body);
            body.setPadding(0,0,0,0);
        }
        else {
            topView.setmContext(getContext());
        }

        TextView english,phonogram,chinese,example;
        Button learned;

        english = v.findViewById(R.id.english);
        phonogram = v.findViewById(R.id.phonogram);
        chinese = v.findViewById(R.id.chinese);
        example = v.findViewById(R.id.example);
        Button t_continue = v.findViewById(R.id.t_continue);
        collection = v.findViewById(R.id.collection);
        learned = v.findViewById(R.id.learned);

        SharedPreferences preferences = getActivity().getSharedPreferences("App_Shared_Data", 0);
        username = preferences.getString("username","");

        english.setText(wordData.getEnglish());
        phonogram.setText(Tools.getYinbiao(wordData.getPhonetic()));
        chinese.setText(Tools.getChinese(wordData.getChinese()));
        example.setText(Html.fromHtml(Tools.gseLizi(wordData.getEnglish(),wordData.getExample())));

        t_continue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reStart();
            }
        });
        learned.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Tools.AddUserData(username,wordData.getMid(),"already");
                if (isFuxi){
                    Toast.makeText(getContext(),"标记成功",Toast.LENGTH_SHORT).show();
                }
                else
                    reStart();
            }
        });

        Drawable collection_icon = getResources().getDrawable(R.drawable.collection);
        final Drawable cicon1 = setDrawableColor(collection_icon,0xFFAAAAAA);
        final Drawable cicon2 = setDrawableColor(collection_icon,0xFFF7BA2A);

        isCollect = Tools.CheckUserData(username,wordData.getMid(),"collect");

        if (isCollect == 1){
            collection.setImageDrawable(cicon2);
        }
        else {
            collection.setImageDrawable(cicon1);
        }

        collection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (username.equals("yk")){
                    Toast.makeText(getActivity(),"请先登陆",Toast.LENGTH_SHORT).show();
                }
                else {
                    if (isCollect == 1){
                        isCollect = 0;
                        Tools.DelUserData(username,wordData.getMid(),"collect");
                        collection.setImageDrawable(cicon1);
                        Toast.makeText(getActivity(),"取消收藏",Toast.LENGTH_SHORT).show();

                    }
                    else{
                        isCollect = 1;
                        Tools.AddUserData(username,wordData.getMid(),"collect");
                        collection.setImageDrawable(cicon2);
                        Toast.makeText(getActivity(),"收藏成功",Toast.LENGTH_SHORT).show();
                    }
                }


            }
        });

        play_sound = v.findViewById(R.id.play_sound);
        play_sound.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (playSound != null){
                    playSound.speak_d();
                    play_sound.setImageDrawable(getActivity().getResources().getDrawable(R.drawable.play_sound));
                    AnimationDrawable animation = (AnimationDrawable) play_sound.getDrawable();
                    animation.start();
                }

            }
        });

        if (isFuxi){
            t_continue.setVisibility(View.GONE);
            buttons.setVisibility(View.VISIBLE);
        }

        return v;
    }

    public void setIsFuxi(boolean isFuxi){
        this.isFuxi = isFuxi;
    }

    private void reStart(){
        Activity activity = getActivity();
        Intent intent = activity.getIntent();
        activity.overridePendingTransition(0, 0);
        activity.finish();
        activity.overridePendingTransition(0, 0);
        startActivity(intent);
    }

    public void setData(WordData wordData) {
        this.wordData = wordData;
    }

    private Drawable setDrawableColor(Drawable drawable,int color) {
        Drawable.ConstantState state = drawable.getConstantState();
        Drawable drawable1 = DrawableCompat.wrap(state == null ? drawable : state.newDrawable()).mutate();
        drawable1.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        DrawableCompat.setTint(drawable1, color);
        return drawable1;
    }

    public interface PlaySound{
        void speak_d();
    }

    public void setPlaySound(PlaySound playSound) {
        this.playSound = playSound;
    }

    @Override
    public void show(@NonNull FragmentManager manager, @Nullable String tag) {
        if (isShow)
            return;
        isShow = true;
        super.show(manager, tag);
    }

    @Override
    public void onDestroy() {
        isShow = false;
        super.onDestroy();
    }

}
